package concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: DRevelation
 * @Date: 2021/1/28 11:19
 */
public class SynchronizedDemo {

    private static ExecutorService exec = Executors.newFixedThreadPool(2);

    private static Integer num = 0;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            exec.submit(() -> {
                try {
                    Thread.sleep(100);
                    //不加锁结果不等于10
                    synchronized (num) {
                        num++;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        Thread.sleep(3000);
        System.out.println("num = " + num);
        exec.shutdown();
    }

}
